//
// This Stan program defines a simple model, with a
// vector of values 'y' modeled as normally distributed
// with mean 'mu' and standard deviation 'sigma'.
//
// Learn more about model development with Stan at:
//
//    http://mc-stan.org/users/interfaces/rstan.html
//    https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started
//

// The input data is a vector 'y' of length 'N'.
data {
  int<lower=0> I;
  int<lower=0> n[I];
  int<lower=0> y[I];
  real eta_star;
}

// The parameters accepted by the model. Our model
// accepts two parameters 'mu' and 'sigma'.
parameters {
  real<lower=0, upper=1> mu;
  real log_eta;
  vector<lower=0, upper=1>[I] p;
}

transformed parameters {
  real<lower=0> eta = exp(log_eta);
}

// The model to be estimated. We model the output
// 'y' to be normally distributed with mean 'mu'
// and standard deviation 'sigma'.
model {
  mu ~ beta(1, 1);
  log_eta ~ logistic(log(eta_star), 1);
  p ~ beta(mu * eta, (1-mu) * eta);
  y ~ binomial(n, p);
}

generated quantities {
  vector[I] y_pred;
  for(i in 1:I){
    y_pred[i] = binomial_rng(n[i], p[i]);
  }
  
}

